// pages/scan/scan.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('code---',options)
    let scan = options.scene;
   if(scan){
     let path = scan.split('?')[0];
     //      存酒-my
     if (path == 'pages/my/my') {
       let wine_no = scan.match(/=(\S*)-/)[1];
       let house_num = scan.split('-')[1];
       wx.request({
         url: app.globalData.baseUrl + '/api/drinks/scanCode',
         method: 'POST',
         header: {
           'content-type': 'application/x-www-form-urlencoded'
         },
         data: {
           session_key: app.gs(),
           house_num: house_num,
           wine_no: wine_no
         },
         success(res) {
           console.log('success', res)
           if (res.data.message == 200) {
             wx.showModal({
               title: '存酒成功',
               content: '请确认',
               success(res) {
                 if (res.confirm) {
                   console.log('用户点击确定')
                   setTimeout(function () {
                     wx.switchTab({
                       url: '../my/my'
                     })
                   }, 1000)
                 } else if (res.cancel) {
                   console.log('用户点击取消')
                   setTimeout(function () {
                     wx.switchTab({
                       url: '../my/my'
                     })
                   }, 1000)
                 }
               }
             })
           } else {
             wx.showToast({
               title: '存酒失败',
             })
           }
         }
       })
     }
   }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  	 
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  saoma(e){
    console.log(e)
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log('res',res)
        let scan = res.path;
        console.log('scan', scan)
        let path = scan.split('?')[0];
        console.log('path',path)
        
        // 确认房间和套餐--scan---1--套餐  ---2 --房间

        if (path =='pages/scan/scan'){
          let scan1 = scan.match(/=(\S*)-/)[1];
          // let scan0 = scan.match(/=(\S*)-/)[0];
          let order_no = scan.match(/-(\S*)/)[1];
          console.log(scan)
          // console.log(scan0,scan1)
          if (scan1==1){
            wx.request({
              url: app.globalData.baseUrl + '/api/commodity/scanCode',
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                session_key: app.gs(),
                order_no: order_no
              },
              success(res) {
                console.log('success', res)


                if (res.data.message == 200) {
                  wx.showModal({
                    title: '确认完成'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }
                if (res.data.message == 404) {
                  wx.showToast({
                    title: '非工作人员'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }

                if (res.data.message == 405) {
                  wx.showToast({
                    title: '二维码已核销'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }
              }
            })
          }else if(scan1==2){
            wx.request({
              url: app.globalData.baseUrl + '/api/house/scanCode',
              method: 'POST',
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              data: {
                session_key: app.gs(),
                order_no: order_no
              },
              success(res) {
                console.log('success', res)


                if (res.data.message == 200) {
                  wx.showModal({
                    title: '确认完成'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }
                if (res.data.message == 404) {
                  wx.showToast({
                    title: '非工作人员'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }

                if (res.data.message == 405) {
                  wx.showToast({
                    title: '二维码已核销'
                  })
                  setTimeout(function () {
                    wx.switchTab({
                      url: '../scan/scan'
                    })
                  }, 2000)
                }
              }
            })
          }
          
        }
//      存酒-my
        if (path == 'pages/my/my') {
          let wine_no = scan.match(/=(\S*)-/)[1];
          let house_num = scan.split('-')[1];
          wx.request({
            url: app.globalData.baseUrl + '/api/drinks/scanCode',
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              session_key: app.gs(),
              house_num: house_num,
              wine_no: wine_no
            },
            success(res) {
              console.log('success', res)


              if (res.data.message == 200) {
                wx.showModal({
                  title: '存酒成功',
                  content:'请确认',
                  success(res){
                    if (res.confirm) {
                      console.log('用户点击确定')
                      setTimeout(function () {
                        wx.switchTab({
                          url: '../my/my'
                        })
                      }, 1000)
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                      setTimeout(function () {
                        wx.switchTab({
                          url: '../my/my'
                        })
                      }, 1000)
                    }
                  }
                  
                })
               
                
              }else {
                wx.showToast({
                  title: '存酒失败',
                })
              }
            }
          })
        }
        //      分配房号-index
        if (path == 'pages/index/index') {
          let house_num = scan.match(/scene=(\S*)/)[1];
          wx.request({
            url: app.globalData.baseUrl + '/api/house/band',
           
            data: {
              session_key: app.gs(),
              house_num: house_num
            },
            success(res) {
              console.log('success', res)


              if (res.data.message == 200) {
                wx.showToast({
                  title: '绑定房间成功'
                })
               
                setTimeout(function () {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }, 2000)
              }
            }
          })
        }
      },
      fail(res) {
        
      }
    })
    
  }
})